from django.db.models import Q
from rest_framework import viewsets
from ERP_system.utils.multiple_destory import MultipleDestoryMixin,MultipleOpenMixin
from ERP_system.utils.pagination import GlobalPagination

from basic_info.models import CustomerModel
from basic_info.serializer.customer_serializer import CustomerSerializer

class CustomerView(viewsets.ModelViewSet,MultipleDestoryMixin,MultipleOpenMixin):

    queryset = CustomerModel.objects.all()

    serializer_class = CustomerSerializer


    pagination_class = GlobalPagination

#搜索框过滤查询
    def get_queryset(self):

        if self.action=='list':
            #接受三个可能的参数
            name=self.request.data.get('name',None)
            phone = self.request.data.get('phone', None)
            mobile = self.request.data.get('mobile', None)

            #因为参数可能没有，造成过滤条件是动态的

            query=Q()

            if name:
                query.add(Q(name__contains=name),'AND')
            if phone:
                query.add(Q(phone__contains=phone),'AND')
            if mobile:
                query.add(Q(mobile__contains=mobile),'AND')
            return CustomerModel.objects.filter(query).all()

        else:

            return CustomerModel.objects.all()











